package com.csjbot.voicesample;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.csjbot.voiceclient.VoiceClientAgent;
import com.csjbot.voiceclient.constant.ClientConstant;
import com.csjbot.voiceclient.listener.ClientEvent;
import com.csjbot.voiceclient.listener.EventListener;
import com.csjbot.voiceclient.utils.VoiceLogger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements EventListener {
    private VoiceClientAgent mAgent;
    private File file;
    private FileOutputStream fos = null;

    private boolean initFile, initAudioOut;
    private AudioTrack trackplayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mAgent = VoiceClientAgent.createRosClientAgent(this, true);

        // TODO: 2018/04/25 0025 要开实时音频输出就打开这个
        initAudio();
        // TODO: 2018/04/25 0025 要写文件就打开这个
//        initFile();
    }

    private void initAudio() {
        initAudioOut = true;

        int bufsize = AudioTrack.getMinBufferSize(16000,//每秒8K个点 
                AudioFormat.CHANNEL_OUT_MONO,//双声道
                AudioFormat.ENCODING_PCM_16BIT);//一个采样点16比特-2个字节

        trackplayer = new AudioTrack(AudioManager.STREAM_MUSIC,
                16000,
                AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                bufsize,
                AudioTrack.MODE_STREAM);

        trackplayer.play();
    }

    private void initFile() {
        initFile = true;

        try {
            // 新建一个当前时间戳的pcm文件，收到数据就不停的写
            file = new File(Environment.getExternalStorageDirectory(), String.valueOf(System.currentTimeMillis()) + ".pcm");
            if (file.exists()) {
                file.delete();
            }
            fos = new FileOutputStream(file);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public void onEvent(ClientEvent event) {
        switch (event.eventType) {
            case ClientConstant.EVENT_RECONNECTED:
            case ClientConstant.EVENT_CONNECT_SUCCESS:
//                connectStatus(ClientConstant.EVENT_CONNECT_SUCCESS);
                VoiceLogger.info("EVENT_CONNECT_SUCCESS");
                break;
            case ClientConstant.EVENT_CONNECT_FAILD:
                VoiceLogger.error("EVENT_CONNECT_FAILD " + String.valueOf(event.data));
                break;
            case ClientConstant.EVENT_CONNECT_TIME_OUT:
                VoiceLogger.error("EVENT_CONNECT_TIME_OUT  " + String.valueOf(event.data));
                break;
            case ClientConstant.SEND_FAILED:
                VoiceLogger.error("SEND_FAILED");
                break;
            case ClientConstant.EVENT_DISCONNET:
                VoiceLogger.error("EVENT_DISCONNET");
                break;
            case ClientConstant.EVENT_PACKET:
                // 收到数据就不停的写
                byte[] data = (byte[]) event.data;
                if (initFile) {
                    try {
                        fos.write(data);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (initAudioOut) {
                    trackplayer.write(data, 0, data.length);
                }
                break;
            default:
                break;
        }

    }

    @Override
    protected void onDestroy() {
        try {
            if (fos != null)
                fos.close();// 关闭写入流
        } catch (IOException e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    public void connectLinux(View view) {
        // TODO: 2018/04/25 0025 这里填写 192.168.99.101
        mAgent.connect("192.168.99.101", 60004);
    }
}
